package com.liuwei.msmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.product.dao.CategoryBrandRelationDao;
import com.liuwei.msmall.product.entity.BrandEntity;
import com.liuwei.msmall.product.entity.CategoryBrandRelationEntity;
import com.liuwei.msmall.product.entity.CategoryEntity;
import com.liuwei.msmall.product.service.BrandService;
import com.liuwei.msmall.product.service.CategoryBrandRelationService;
import com.liuwei.msmall.product.service.CategoryService;
import com.liuwei.msmall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    CategoryService categoryService;
    @Resource
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        if (!Objects.isNull(params.get("brandId"))){
            queryWrapper.eq("brand_id",params.get("brandId").toString());
        }

        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity byId = brandService.getById(brandId);
        CategoryEntity byId1 = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(byId.getName());
        categoryBrandRelation.setCatelogName(byId1.getName());
        this.save(categoryBrandRelation);
    }
    @Override
    public List<BrandVo> getBrands(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = this.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id ", catId));
        if (CollectionUtils.isEmpty(relationEntities)) return Lists.newArrayList();

        List<BrandVo> brandVoList = relationEntities.stream().map(this::convertVo).collect(Collectors.toList());
        return brandVoList;
    }
    public BrandVo convertVo(CategoryBrandRelationEntity entity){
        BrandVo brandVo = new BrandVo();
        BeanUtils.copyProperties(entity,brandVo);
        return brandVo;
    }

}
